//
//  GiftViewController.m
//  GiftTalk
//
//  Created by justaway on 15/8/14.
//  Copyright (c) 2015年 swiftyper. All rights reserved.
//

#import "GiftViewController.h"
#import "ApiClient.h"
#import "AppConstants.h"
#import "GiftCell.h"

@interface GiftViewController ()

@property (nonatomic, strong) NSMutableArray *gifts;

@end

@implementation GiftViewController

- (NSMutableArray *)gifts {
    if (_gifts == nil) {
        _gifts = [NSMutableArray array];
    }
    return _gifts;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initCollectinView];
    
    [[ApiClient sharedInstance] getGiftsWithGender:1 GenerationType:1 completion:^(NSArray *gifts, NSError *error) {
        
        if (error == nil) {
            
            [self.gifts addObjectsFromArray:gifts];
            [self.collectionView reloadData];
            
        } else {
            NSLog(@"获得单品数据错误: %@", error);
        }
        
    }];
    
}

- (void)initCollectinView {
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    
    CGFloat cellWidth = (ScreenBounds.size.width - GiftCellMargin * 3) / 2;
    layout.itemSize = CGSizeMake(cellWidth, GiftCellHeight);
}


#pragma mark UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.gifts.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * const ReuseableIdentifier = @"GiftCell";
    
    GiftCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ReuseableIdentifier forIndexPath:indexPath];
    
    Gift *gift = self.gifts[indexPath.row];
    cell.gift = gift;
    
    return cell;
}

#pragma mark UICollectionViewDelegate

@end
